interface Info {
	cip: string;
	cid: string;
	cname: string;
}

/**
 * 获取ip
 * @return JSON
 * @example const res=await get_ip();
 */
export default async function get_ip() {
	return new Promise<Info>((resolve, reject) => {
		if ((window as any).returnCitySN) {
			resolve((window as any).returnCitySN);
			return;
		}
		const script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', 'http://pv.sohu.com/cityjson?ie=utf-8');
		const heads = document.getElementsByTagName('head');
		if (heads.length) {
			heads[0].appendChild(script);
		} else {
			document.documentElement!.appendChild(script);
		}
		script.onload = (() => {
			resolve((window as any).returnCitySN);
		});
		script.onerror = ((err) => {
			reject(err);
		});
	});
}
